package com.sphd.emanu.fragment.cardnum;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.daydetail.DayDetailActivity;
import com.sphd.emanu.adapter.CardNumFragmentAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.interfaces.CardFragmentLetter;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.bank.AccountPeriodListByAccountBean;
import com.sphd.emanu.pojo.bank.CardNumMessage;
import com.sphd.emanu.pojo.login.User;
import com.wzutils.TimeUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @Author Hu
 * @Time 2017/7/19 -- 15:30.
 * @Email 87683202@qq.com
 */

public class CardNumFragment extends MVPBaseFragment<CardNumContract.View, CardNumPresenter> implements CardNumContract.View {

    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Bundle bundle;
    Unbinder unbinder;
    CardFragmentLetter cardFragmentLetter;
    CardNumFragmentAdapter cardNumFragmentAdapter;
    int state;
    User user;
    String beginDate, endDate;
    Intent intent;
    public void setCardFragmentLetter(CardFragmentLetter cardFragmentLetter) {
        this.cardFragmentLetter = cardFragmentLetter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.myswf_and_recycler;
    }

    @Override
    protected void initView() {
        intent=new Intent();
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        initEvent();
        setRefresh(mySwf);
        initAdapter();
        cardFragmentLetter.readyedCardFragment();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> addData());
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                AccountPeriodListByAccountBean accountPeriodListByAccountBean = (AccountPeriodListByAccountBean) adapter.getItem(position);
                accountPeriodListByAccountBean.setState(state);
                bundle.putSerializable(Constant.ACCOUNT_BEAN, accountPeriodListByAccountBean);
                bundle.putString(Constant.FLAG_NAME, CardNumFragment.class.getName());
                bundle.putInt(Constant.ID,accountPeriodListByAccountBean.getAccountId().getId());
                bundle.putString(Constant.Salary_User_Start,beginDate);
                bundle.putString(Constant.Salary_User_End,endDate);
                intent.putExtras(bundle);
                intent.setClass(getActivity(), DayDetailActivity.class);
                startActivity(intent);





            }
        });
    }

    private void initAdapter() {
        setRecy(myRecycler);
        cardNumFragmentAdapter = new CardNumFragmentAdapter(R.layout.recycler_io_coard, new ArrayList<AccountPeriodListByAccountBean>(),
                bundle.getBoolean(Constant.ShowIncome), bundle.getBoolean(Constant.ShowExpend));
        cardNumFragmentAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(cardNumFragmentAdapter);

    }


    @Override
    protected void initData() {

    }


    @Override
    public void setBean(CardNumMessage body) {
        dissProgressDialog();
        cardNumFragmentAdapter.setNewData(body.getAccountPeriodListByAccount());
    }

    public void swipeRefreshData(int state, String beginDate, String endDate) {
        this.state = state;
        this.endDate = endDate;
        this.beginDate = beginDate;
        addData();
    }

    private void addData() {
        showProgressDialog();
        mPresenter.getAccountPeriodListByAccount(setMapMessage());
    }

    public Map setMapMessage() {
        Map<String, String> map = new HashMap<>();
        map.put("oid", String.valueOf(user.getOid()));
        map.put("state", String.valueOf(state));
        map.put("monthOrDay", "" + (
                TimeUtils.isOneYear(beginDate,
                        endDate) ?
                        (TimeUtils.isOneMonth(beginDate,
                                endDate) ? 2 : 1) : 1)
        );
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return map;
    }
}
